package com.example.textfolddemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class TextFold extends View {
    public TextFold(Context context) {
        this(context, null);
    }

    public TextFold(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextFold(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
        init();

    }

    public TextFold(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    //内边距
    private int padding_left = 20;
    private int padding_right = 20;
    private int padding_top = 20;
    private int padding_bottom = 20;

    private int Maxilien = 3;//最大行
    private String texts = "《海贼王》漫画1050话迎来了更新，由海外大佬发布的先行图已经出现，观看后，能感受到鬼岛篇彻底完结，新的危机来到和之国，而击败凯多的路飞，他的功劳却被赤鞘九侠抢走，让我们见识到这尴尬的武士文化，难道乌索普和我一点都不喜欢。#海贼王#上一话路飞用猿王枪击落凯多，结尾出现他失去意识的分镜，来到1050话，他整个人都陷入昏迷状态，在半空中自动结束锁血挂·尼卡形态，然后在大和的接住下，安全落地。\n" +
            "\n" +
            "从这里来看，大和上船稳了？不！鬼知道尾田的想法是什么，毕竟他可能会让必死桃之助上船，而不是大和。";

    private float textsize = 50;//文字大小

    private boolean FoleBool = true;


    private Paint paint;
    private Paint foldpaint;
    private void init() {
        paint = new Paint();
        paint.setColor(Color.parseColor("#000000"));
        paint.setTextSize(textsize);
        paint.setAntiAlias(true);
        paint.setDither(true);
        foldpaint = new Paint();
        foldpaint.setColor(Color.parseColor("#0066ff"));
        foldpaint.setTextSize(textsize);
        foldpaint.setAntiAlias(true);
        foldpaint.setDither(true);
    }

    private int parent_width;
    private int parent_height;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int w = widthMeasureSpec;
        int h = heightMeasureSpec;
        parent_width = resolveSize(w, widthMeasureSpec);
        parent_height = resolveSize(h, heightMeasureSpec);
        setMeasuredDimension(parent_width, parent_height);
        Log.e("EEE",parent_height+"");
        Log.e("EEE",parent_width+"");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        char[] chars = texts.toCharArray();
        int fontsize = CountLineNumber();
        int lensize =  CountLineSize();
        if(FoleBool)
        {
            FoldText(fontsize,lensize,canvas,chars);
        }else {
            Launch(fontsize,lensize,canvas,chars);
        }
    }

    /**
     * 折叠绘制
     * @param fontsize
     * @param lensize
     * @param canvas
     * @param chars
     */
    private float folex;//X轴
    private float foley;//Y轴
    private void FoldText(int fontsize,int lensize,Canvas canvas,char[] chars)
    {
        Log.e("行字符数",fontsize+"");
        Log.e("列数量",lensize+"");
        int s = 0;
        int h = 0;
        for(int i=0;i<chars.length;i++)
        {

            if(h>=fontsize)
            {
                s++;
                h = 0;
            }
            float x = CountX(h);
            float y = CountY(s);
            if(s>Maxilien-1)
            {
                folex = x;
                foley = y;
                canvas.drawText("展开",x,y,foldpaint);
                return;
            }

            Log.e("EEEy",y+"");
            canvas.drawText(chars[i]+"",x,y,paint);
            Log.e("EEEi",i+"");

            h++;
        }
    }

    /**
     * 展开绘制
     * @param fontsize
     * @param lensize
     * @param canvas
     * @param chars
     */
    private void Launch(int fontsize,int lensize,Canvas canvas,char[] chars)
    {
        Log.e("行字符数",fontsize+"");
        Log.e("列数量",lensize+"");
        int s = 0;
        int h = 0;
        for(int i=0;i<chars.length;i++)
        {

            if(h>=fontsize)
            {
                s++;
                h = 0;
            }
            float x = CountX(h);
            float y = CountY(s);

            Log.e("EEEy",y+"");
            canvas.drawText(chars[i]+"",x,y,paint);
            Log.e("EEEi",i+"");
            h++;
        }
        float[] pix = LaunchNumber(chars.length,fontsize);
        canvas.drawText("收起",pix[1],pix[0],foldpaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getActionMasked();
        if(action==MotionEvent.ACTION_DOWN)
        {
            float x = event.getX();
            float y = event.getY();
            boolean b = DownPix(x, y);
            if(b)
            {
                if(FoleBool)
                {
                    FoleBool = false;
                    invalidate();
                }else {
                    FoleBool = true;
                    invalidate();
                }
            }
        }

        return super.onTouchEvent(event);
    }


    private float[] LaunchNumber(int lengths,int fontsize)
    {
        int n =  lengths / fontsize;
        int v =  lengths % fontsize;
        if(v>0)
        {
            n++;
        }
        int x = 0;
        int y = (int) (n*textsize) + padding_top;
        if(v>fontsize-2){
            y = (int) ((int) (n*textsize) + padding_top + textsize);
            x = (int) ((0*textsize) + padding_left);
        }else {
            y = ((int) (n*textsize) + padding_top );
            x = (int) ((v*textsize) + padding_left);
        }

        folex = x;
        foley = y;
        return new float[]{y,x};
    }


    /**
     * 点击位置判断
     * @param x
     * @return
     */
    private boolean DownPix(float x,float y)
    {
        int i = 0;
        if(x<= (folex+(textsize*2)) && x>= (folex-(textsize*2)))
        {
            i++;
        }
        if(y<=(foley+(textsize*2)) && y>=(foley-(textsize*2)))
        {
            i++;
        }
        return i>1?true:false;
    }


    /**
     * 计算出每个字的X轴坐标
     * @param index 字数 0开始
     * @return
     */
    private float CountX(int index) {
        float x = padding_left + (index * textsize);
        return x;
    }

    /**
     * 计算除每个字的Y轴坐标
     * @return
     */
    private float CountY(float current) {
        float y = padding_top + (current * textsize)+textsize;
        return y;
    }

    /**
     * 计算每行字符数量
     * @return
     */
    private int CountLineNumber() {
        int w = parent_width - (padding_left + padding_right);
        int s = (int) (w / textsize);
        return s;
    }

    /**
     * 计算可绘制多少行
     * @return
     */
    private int CountLineSize()
    {
        int h = parent_height - (padding_top + padding_bottom);
        int s = (int) (h / textsize);
        return s;
    }





}
